package com.hcrazy.manchester.app.controllers.report;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.NewsInfoService;
import com.hcrazy.manchester.app.services.NewsSortService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/api/report")
public class ReportAPIController {
	
	@Autowired
	private NewsInfoService newsInfoService;
	
	@Autowired
	private NewsSortService newsSortService;

	@RequestMapping(value="/sorts",method=RequestMethod.GET)
	public @ResponseBody Object getSorts(){
		try {
			return newsSortService.getAllSorts(Constant.NEWS_TYPE_REPORT, 0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public @ResponseBody Object getChannels(
			@RequestParam(required=false) Integer sortId,
			@RequestParam(required=false) Integer pageNum,
			@RequestParam(required=false) Integer numPerPage,
			@RequestParam(required=false) String search){
		try {
			if(sortId == null)
				sortId = 0;
			
			if(numPerPage == null)
				numPerPage = 8;
			if(pageNum == null)
				pageNum = 1;
			
			if(numPerPage != null && numPerPage == 0){
				return newsInfoService.listAllInfos(Constant.NEWS_TYPE_REPORT);
			}
			
			PageModel model = new PageModel();
			model.setPageNum(pageNum);
			model.setNumPerPage(numPerPage);
			
			return newsInfoService.getAddressInfos(model, search, sortId, Constant.NEWS_TYPE_REPORT);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	@RequestMapping(value="/info",method=RequestMethod.GET)
	public @ResponseBody Object getReportById(@RequestParam int newsId){
		try {
			return newsInfoService.getInfoById(newsId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
			
	
}
